﻿using System;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using DevExpress.CrmDemo.Win.Data;
using DevExpress.CrmDemo.Win.Controls;
using System.Windows.Forms;
using DevExpress.CrmDemo.Win.Helpers;

namespace DevExpress.CrmDemo.Win {
    public enum States {
        None,
        StartPage,
        Dashboard,
        OppDetail,
        ReportViewer,
        Scheduler,
        UnderConstruction,
        AdminIndex,
        CommonOrderList,
        AdminManager,
        CompanyManager,
        BrandManager,
        CompanyPayMoney,
        CompanyInMoney,
        InlineOrder,
        GoodsAdd,
        SystemConfig,
        UpdatePassword,
        CustomMangager
    }

    public partial class MainForm : DevExpress.XtraEditors.XtraForm {
        public MainForm() {
            InitializeComponent();
            ColorHelper.MainForm = this;
        }

       

       


        private void MainForm_Load(object sender, EventArgs e) {
            Rectangle rect = new Rectangle();
            rect = Screen.GetWorkingArea(this);
            this.Size = DevExpress.Utils.ScaleUtils.GetScaleSize(new System.Drawing.Size(rect.Width,rect.Height));
            var preloadData = DataProvider.Instance;

            var preloadDashboard = MyDashboard;

            State = States.StartPage;
            this.MaximizeBox = true;
            
        }

        private Dashboard myDashboard;

        public Dashboard MyDashboard {
            get {
                if (myDashboard == null) {
                    myDashboard = new Dashboard();
                    myDashboard.Dock = System.Windows.Forms.DockStyle.Fill;
                    myDashboard.NavRequest += new NavRequestEventHandler(NavRequestHandler);
                    myDashboard.Opportunities = DataProvider.Instance.Opportunities;
                }

                return myDashboard;
            }
        }

        private OppDetail myDetail;

        public OppDetail MyDetail {
            get {
                if (myDetail == null) {
                    myDetail = new OppDetail();
                    myDetail.Dock = System.Windows.Forms.DockStyle.Fill;
                    myDetail.NavRequest += new NavRequestEventHandler(NavRequestHandler);
                }

                return myDetail;
            }
        }

        private Start myStart;

        public Start MyStart {
            get {
                if (myStart == null) {
                    myStart = new Start();
                    myStart.Dock = System.Windows.Forms.DockStyle.Fill;
                    myStart.NavRequest += new NavRequestEventHandler(NavRequestHandler);
                }

                return myStart;
            }
        }
 

        private UnderConstruction myUnderConstruction;

        public UnderConstruction MyUnderConstruction {
            get {
                if(myUnderConstruction == null) {
                    myUnderConstruction = new UnderConstruction();
                    myUnderConstruction.Dock = System.Windows.Forms.DockStyle.Fill;
                    myUnderConstruction.NavRequest += new NavRequestEventHandler(NavRequestHandler);
                }

                return myUnderConstruction;
            }
        }

        private AdminIndex myAdminIndex;

        public AdminIndex MyAdminIndex
        {
            get
            {
                if (myAdminIndex == null)
                {
                    myAdminIndex = new AdminIndex();
                    myAdminIndex.Dock = System.Windows.Forms.DockStyle.Fill;
                    myAdminIndex.NavRequest += new NavRequestEventHandler(NavRequestHandler);
                }

                return myAdminIndex;
            }
        }

        private CommonOrderList myCommonOrderList;

        public CommonOrderList MyCommonOrderList
        {
            get
            {
                if (myCommonOrderList == null)
                {
                    myCommonOrderList = new CommonOrderList();
                    myCommonOrderList.Dock = System.Windows.Forms.DockStyle.Fill;
                    myCommonOrderList.NavRequest += new NavRequestEventHandler(NavRequestHandler);
                }

                return myCommonOrderList;
            }
        }



        private ReportPage myReportPage;

        public ReportPage MyReportPage {
            get {
                if (myReportPage == null) {
                    myReportPage = new ReportPage();
                    myReportPage.Dock = System.Windows.Forms.DockStyle.Fill;
                    myReportPage.NavRequest += new NavRequestEventHandler(NavRequestHandler);
                }

                return myReportPage;
            }
        }

        private SchedulerPage mySchedulerPage;

        public SchedulerPage MySchedulerPage {
            get {
                if (mySchedulerPage == null) {
                    mySchedulerPage = new SchedulerPage(DataProvider.Instance.Appointments, DataProvider.Instance.TeamMembers);
                    mySchedulerPage.Dock = System.Windows.Forms.DockStyle.Fill;
                    mySchedulerPage.NavRequest += new NavRequestEventHandler(NavRequestHandler);
                }

                return mySchedulerPage;
            }
        }



        private CommonPage myCommonPage;

        public CommonPage MyCommonPage
        {
            get
            {
    
                    myCommonPage = new CommonPage();
                    myCommonPage.Dock = System.Windows.Forms.DockStyle.Fill;
                    myCommonPage.NavRequest += new NavRequestEventHandler(NavRequestHandler);
             
                return myCommonPage;
            }
        }




        void NavRequestHandler(object sender, NavRequestEventArgs e) {
            OpportunityId = e.OpportunityId;
            State = e.TargetState;
        }

        private States state;

        private States State {
            get { return state; }
            set {
                if (state != value) {
                    state = value;
                    LoadPage(state);
                }
            }
        }

        private int OpportunityId { get; set; }

        void LoadPage(States state) {
            Control newPage = null;

            switch (state) {
                case States.StartPage:
                    newPage = MyStart;
                    break;
          
                case States.AdminIndex:
                    newPage = MyAdminIndex;
                    break;

                case States.CommonOrderList:
                       BasePage.PageStates = States.CommonOrderList;
                       newPage = MyCommonPage;
                    break;

                case States.AdminManager:
                    BasePage.PageStates = States.AdminManager;
                    newPage = MyCommonPage;
                    break;
                case States.CompanyManager:
                      BasePage.PageStates = States.CompanyManager;
                    newPage = MyCommonPage;
                    break;

                case States.BrandManager:
                    BasePage.PageStates = States.BrandManager;
                    newPage = MyCommonPage;
                    break;
                case States.CompanyPayMoney:
                       BasePage.PageStates = States.CompanyPayMoney;
                    newPage = MyCommonPage;
                    break;
                case States.UpdatePassword:
                    BasePage.PageStates = States.UpdatePassword;
                    newPage = MyCommonPage;
                    break;
                case States.GoodsAdd:
                    BasePage.PageStates = States.GoodsAdd;
                    newPage = MyCommonPage;
                    break;
                case States.SystemConfig:
                    BasePage.PageStates = States.SystemConfig;
                    newPage = MyCommonPage;
                    break;
                case States.CustomMangager:
                    BasePage.PageStates = States.CustomMangager;
                    newPage = MyCommonPage;
                    break;
                case States.InlineOrder:
                    BasePage.PageStates = States.InlineOrder;
                    newPage = MyCommonPage;
                    break;
                case States.CompanyInMoney:
                    BasePage.PageStates = States.CompanyInMoney;
                    newPage = MyCommonPage;
                    break;
                default:
                    break;
            }

            if (newPage != null) {
                this.Controls.Clear();
                this.Controls.Add(newPage);
            }
        }
    }

    public delegate void NavRequestEventHandler(object sender, NavRequestEventArgs e);

    public class NavRequestEventArgs : EventArgs {
        public NavRequestEventArgs(States targetState) {
            this.TargetState = targetState;
        }

        public NavRequestEventArgs(States targetState, int opportunityId) {
            this.TargetState = targetState;
            this.OpportunityId = opportunityId;
        }

        public States TargetState { get; set; }
        public int OpportunityId { get; set; }
    }
}
